package com.example.orchid.paper.fragment;

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.example.orchid.paper.R;
import com.example.orchid.paper.util.PhoneUtils;


/**
 * 设置界面.
 */
public class PrefsFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener, Preference.OnPreferenceChangeListener {
    private static final String PREFERENCES_ABOUT = "about";
    private static final String PREFERENCE_VERSION = "version";
    private static final String PREFERENCE_NOIMAGE_NOWIFI = "noimage_nowifi?";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addPreferencesFromResource(R.xml.fragment_prefs);

        findPreference(PREFERENCES_ABOUT).setOnPreferenceClickListener(this);
        findPreference(PREFERENCE_VERSION).setOnPreferenceClickListener(this);
        findPreference(PREFERENCE_NOIMAGE_NOWIFI).setOnPreferenceChangeListener(this);

        boolean noImageWifi = PreferenceManager.getDefaultSharedPreferences(getActivity()).
                getBoolean(PREFERENCE_NOIMAGE_NOWIFI, false);
        findPreference(PREFERENCE_NOIMAGE_NOWIFI).setDefaultValue(noImageWifi);
    }

    @Override
    public boolean onPreferenceClick(Preference preference) {
        if (preference.getKey().equals(PREFERENCES_ABOUT)) {
            showDialog(false);
        } else if (preference.getKey().equals(PREFERENCE_VERSION)) {
            showDialog(true);
        }
        return false;

    }

    /**
     * @param isVersion 是否为显示版本号
     */
    private void showDialog(boolean isVersion) {
        final Dialog dialog = new Dialog(getActivity());
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.dialog_version);

        TextView textView = (TextView) dialog.findViewById(R.id.dialog_text);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        if (isVersion) {
            String data = getResources().getString(R.string.setting_aboutme_version);

            data = String.format(data,
                    PhoneUtils.getApplicationName(getActivity()),
                    PhoneUtils.getPackageInfo(getActivity()).versionName);

            textView.setText(data);
        } else {
            String title = new StringBuilder().append(PhoneUtils.getApplicationName(getActivity())).
                    append("<br/>").toString();
            String subTitle = new StringBuilder().append(getResources().
                    getString(R.string.app_sub_name)).append("<br/>").toString();
            String author = new StringBuilder().append("@").
                    append(getResources().getString(R.string.app_author)).toString();

            String data = getResources().getString(R.string.setting_aboutme_text);

            data = String.format(data,
                    title,
                    subTitle,
                    author);
            CharSequence charSequence = Html.fromHtml(data);

            textView.setText(charSequence);
        }

        textView.setMovementMethod(LinkMovementMethod.getInstance());
        dialog.show();
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (preference.getKey().equals(PREFERENCE_NOIMAGE_NOWIFI)) {

            if (newValue instanceof Boolean) {
                Boolean flag = (Boolean) newValue;

                SharedPreferences mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
                SharedPreferences.Editor mEditor = mPreferences.edit();
                mEditor.putBoolean(PREFERENCE_NOIMAGE_NOWIFI, flag);
                mEditor.apply();
            }
            return true;
        }
        return false;
    }


}
